// components/coupon/coupon.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    //优惠券标识
    couponId:{
      type:String,
      value:'1'
    },
    //价钱
    couponPrice: {
      type: String,
      value: '30'
    },
    couponName: {
      type: String,
      value: '运费抵用卷'
    },
    descript: {
      type: String,
      value: '已过期'
    },
    ruleName: {
      type: String,
      value: '每月订单可用'
    },
    startDate: {
      type: Date,
      value: '2021-7-30'
    },
    endDate: {
      type: Date,
      value: '2021-8-30'
    },
    isUsable:{
      type:Boolean,
      value:true
    },

  },

  /**
   * 组件的初始数据
   */
  data: {
    imgPath: '/images',
    //时间
    dtCoupon: '2021.7.30 - 2021.8.30',
  },

  observers: {
    startDate: function (val) {
      this.setDtCoupon()
    },
    endDate:function(val){
      this.setDtCoupon()
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    setDtCoupon() {
      let startDate=new Date(this.data.startDate)
      let endDate=new Date(this.data.endDate)
      // console.log("startDate",startDate,"endDate",endDate)

      let year1 = startDate.getFullYear();
      let month1 = startDate.getMonth() + 1;
      let date1 = startDate.getDate();

      let year2 = endDate.getFullYear();
      let month2 = endDate.getMonth() + 1;
      let date2 = endDate.getDate();

      this.setData({
        dtCoupon: '' + year1 + '.' + month1 + '.' + date1 + ' - ' + year2 + '.' + month2 + '.' + date2
      })
    },
    /**
     * 使用事件
     * @param {*} e 
     */
    useCoupon(e){
      // console.log("useCoupon",e)
      this.triggerEvent("onUsingCoupon",{couponId:this.data.couponId,couponPrice:this.data.couponPrice,couponName:this.data.couponName,ruleName:this.data.ruleName,startDate:this.data.startDate,endDate:this.data.endDate})
    }
  },
  lifetimes: {
    attached() {
      const util = require("../../utils/util")
      this.setData({
        imgPath:util.getImgPath()
      })
    }
  }
})